import os

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSignal, Qt
from PyQt5.QtGui import QPalette, QColor
from PyQt5.QtWidgets import QApplication, QWidget, QHeaderView
from modules import *
from components import *
import resources_rc

class AlbumDetail(QWidget):
    def __init__(self):
        super().__init__()
        self.setupUi()
        # self.initStyle()
        # if AlbumId:
        #     self.loadData(AlbumId)

    def setupUi(self):
        self.setObjectName("AlbumDetailWidget")
        self.resize(779, 525)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.sizePolicy().hasHeightForWidth())
        self.setSizePolicy(sizePolicy)
        self.setStyleSheet("#AlbumContent{\n"
                                        "    background-color:rgb(255,255,255);\n"
                                        "}")
        self.verticalLayout = QtWidgets.QVBoxLayout(self)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setSpacing(0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.AlbumTitleBox = QtWidgets.QFrame(self)
        self.AlbumTitleBox.setMaximumSize(QtCore.QSize(16777215, 230))
        self.AlbumTitleBox.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.AlbumTitleBox.setFrameShadow(QtWidgets.QFrame.Raised)
        self.AlbumTitleBox.setObjectName("AlbumTitleBox")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.AlbumTitleBox)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setSpacing(0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.AlbumDetailCover = QtWidgets.QLabel(self.AlbumTitleBox)
        self.AlbumDetailCover.setMaximumSize(QtCore.QSize(230, 230))
        self.AlbumDetailCover.setText("")
        self.AlbumDetailCover.setPixmap(QtGui.QPixmap(":/images/images/images/2.jpg"))
        self.AlbumDetailCover.setScaledContents(True)
        self.AlbumDetailCover.setMargin(20)
        self.AlbumDetailCover.setObjectName("AlbumDetailCover")
        self.horizontalLayout.addWidget(self.AlbumDetailCover)
        self.AlbumDetailInfo = QtWidgets.QFrame(self.AlbumTitleBox)
        self.AlbumDetailInfo.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.AlbumDetailInfo.setFrameShadow(QtWidgets.QFrame.Raised)
        self.AlbumDetailInfo.setObjectName("AlbumDetailInfo")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.AlbumDetailInfo)
        self.verticalLayout_2.setSpacing(0)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.AlbumName = QtWidgets.QLabel(self.AlbumDetailInfo)
        self.AlbumName.setMinimumSize(QtCore.QSize(0, 55))
        self.AlbumName.setMaximumSize(QtCore.QSize(16777215, 55))
        font = QtGui.QFont()
        font.setPointSize(20)
        font.setBold(True)
        font.setWeight(75)
        self.AlbumName.setFont(font)
        self.AlbumName.setText("劳动之余")
        self.AlbumName.setObjectName("AlbumName")
        self.verticalLayout_2.addWidget(self.AlbumName)
        self.ArtistName = QtWidgets.QLabel(self.AlbumDetailInfo)
        self.ArtistName.setMinimumSize(QtCore.QSize(0, 25))
        self.ArtistName.setMaximumSize(QtCore.QSize(16777215, 25))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.ArtistName.setFont(font)
        self.ArtistName.setText("歌手： 声音玩具")
        self.ArtistName.setObjectName("ArtistName")
        self.verticalLayout_2.addWidget(self.ArtistName)
        self.ReleaseTime = QtWidgets.QLabel(self.AlbumDetailInfo)
        self.ReleaseTime.setMinimumSize(QtCore.QSize(0, 25))
        self.ReleaseTime.setMaximumSize(QtCore.QSize(16777215, 25))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.ReleaseTime.setFont(font)
        self.ReleaseTime.setText("时间： 2021")
        self.ReleaseTime.setObjectName("ReleaseTime")
        self.verticalLayout_2.addWidget(self.ReleaseTime)
        self.frame_3 = QtWidgets.QFrame(self.AlbumDetailInfo)
        self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_3.setObjectName("frame_3")
        self.verticalLayout_2.addWidget(self.frame_3)
        self.horizontalLayout.addWidget(self.AlbumDetailInfo)
        self.verticalLayout.addWidget(self.AlbumTitleBox)
        self.AlbumContent = QtWidgets.QFrame(self)
        self.AlbumContent.setStyleSheet("")
        self.AlbumContent.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.AlbumContent.setFrameShadow(QtWidgets.QFrame.Raised)
        self.AlbumContent.setObjectName("AlbumContent")
        self.AlbumContentLayout = QtWidgets.QVBoxLayout(self.AlbumContent)
        self.AlbumContentLayout.setContentsMargins(20, 10, 20, 10)
        self.AlbumContentLayout.setSpacing(0)
        self.AlbumContentLayout.setObjectName("AlbumContentLayout")

        self.tableWidget = TableWidget('album')
        self.AlbumContentLayout.addWidget(self.tableWidget)
        self.verticalLayout.addWidget(self.AlbumContent)

        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)

    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("AlbumDetailWidget", "Form"))
        self.tableWidget.setSortingEnabled(False)

    def loadData(self,AlbumId):

        print(AlbumId)
        albumInfo = LocalMusic.getAlbumInfo(self,AlbumId)
        songsList = LocalMusic.getAlbumSongsList(self,albumInfo[1],albumInfo[2])
        self.tableWidget.loadSongsList(songsList)
        cover = Config.coverPath + albumInfo[2] + '-' + albumInfo[1] + '.jpg'
        if os.path.exists(cover):
            r, g, b = DominantColor.getDominantColor(cover)
            palette = QPalette()
            palette.setColor(self.AlbumTitleBox.backgroundRole(), QColor(r, g, b))
            self.AlbumTitleBox.setAutoFillBackground(True)
            self.AlbumTitleBox.setPalette(palette)
            self.AlbumDetailCover.setPixmap(QtGui.QPixmap(cover))

            CommonSignal.changeTitleColorSignal.emit(QColor(r, g, b))

        self.ArtistName.setText("歌手： "+ albumInfo[2])
        self.AlbumName.setText(albumInfo[1])
        self.ReleaseTime.setText("时间： " + albumInfo[3])
        print('------AlbumInfo----')
        print(albumInfo)

